//模拟网络延迟时间的插件
import { delay } from 'roadhog-api-doc';
//一个mock插件，可快速生成mock数据 详情查看：http://mockjs.com/
import Mock from 'mockjs';

//模拟的json数据
import json from './matchingQuestion';

const treeList = [
  {
    id: '0', topId: -1, orgName: '万科集团', explain: '万科集团~~说明~~', key: '0-0', url: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png', children: [
      {
        id: '1', topId: 1, orgName: '南方区域', explain: '南方区域~~~说明~~', key: '0-0-0', url: 'https://gw.alipayobjects.com/zos/rmsportal/dURIMkkrRFpPgTuzkwnB.png', children: [
          { id: '2', topId: 1, orgName: '深圳', explain: '深圳~~说明~~', key: '0-0-0-0', url: 'https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png', },
          {
            id: '3', topId: 1, orgName: '广州', explain: '广州~~说明~~', key: '0-0-0-1', url: 'https://gw.alipayobjects.com/zos/rmsportal/sfjbOqnsXXJgNCjCzDBL.png', children: [
              { id: '4', topId: 1, orgName: '广州万科物业', explain: '广州万科物业~~说明~~', key: '0-0-0-0-0', url: 'https://gw.alipayobjects.com/zos/rmsportal/nxkuOJlFJuAUhzlMTCEe.png', },
              { id: '5', topId: 1, orgName: '广州万科地产', explain: '广州万科地产~~说明~~', key: '0-0-0-0-1', url: 'https://gw.alipayobjects.com/zos/rmsportal/sfjbOqnsXXJgNCjCzDBL.png', },
            ]
          }
        ]
      }, { id: '6', topId: 1, orgName: '上海区域', explain: '上海区域~~说明~~', key: '0-0-1', url: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png', }
    ]
  }
]

function getFakeList(req, res) {
  const params = req.query;
  const result = {
    code: 1,
    msg: 'mock数据成功',
    data: treeList
  };
  console.log("-----------get-----------")
  console.log(treeList)
  return res.json(result);
}

function postFakeList(req, res) {
  // console.log(req.query)
  const { /* url = '', */  body } = req;
  const { method, treeData } = body;
  let result = treeList;
  const aton = '666'
  switch (method) {
    case 'add':
      function fundChildren(data, keys) {
        for (let item of data) {
          if (item.key == keys) {
            item.children.push(treeData)
            break
          }
          if (item.key !== keys && item.children && item.children.length !== 0) {
            fundChildren(item.children, keys)
          }
        }
      }
      fundChildren(treeList, treeData.keys)
      console.log("--------post  add---------")
      console.log(treeList[0].children)
      break;
    case 'delete':
      Array.prototype.remove = function (dx) {
        if (isNaN(dx) || dx > this.length) {
          return false;
        }
        for (var i = 0, n = 0; i < this.length; i++) {
          if (this[i] != this[dx]) {
            this[n++] = this[i]
          }
        }
        this.length -= 1
      }
      function fundChildren(data, keys) {
        for (let item in data) {
          if (data[item].key == keys) {
            data.remove(item)
            break
          }
          if (data[item].key !== keys && data[item].children && data[item].children.length !== 0) {
            fundChildren(data[item].children, keys)
          }
        }
      }
      fundChildren(treeList, treeData.keys)
      break
      break;
    case 'edit':
      function fundChildren(data, keys) {
        for (let item of data) {
          if (item.key == keys) {
            item = treeData
            break
          }
          if (item.key !== keys && item.children && item.children.length !== 0) {
            fundChildren(item.children, keys)
          }
        }
      }
      fundChildren(treeList, treeData.keys)
      break
    default:
      break
  }
  result = {
    code: 1,
    msg: 'mock数据成功',
    data: result
  }
  return res.json(result);

}

const proxy = {
  // 支持值为 Object 和 Array
  'GET /api/users': { users: [1, 2] },

  // GET POST 可省略
  // '/api/users/1': { id: 1 },
  '/api/users/1': Mock.mock({
    code: 1,
    msg: 'mock数据成功',
    'data|30': [{ name: '@city', 'value|1-100': 50, 'type|0-2': 1, 'id|+1': 0 }]
  }),

  // 支持自定义函数，API 参考 express@4
  'POST /api/users/create': (req, res) => {
    res.end('OK');
  },




  'GET /api/tree_list': getFakeList,
  'POST /api/tree_list': postFakeList,
};

// 调用 delay 函数，统一处理 延迟  。若不需要延迟，改成 export default proxy
// export default delay(proxy, 0);
export default proxy;

